%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part a
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
d=28^2;
dP=49;

G=normrnd(0,1/sqrt(dP),dP,d);

%compute mean squared difference
msd=sum(sum((G'*G-eye(d,d)).^2))/(d^2);

disp(['Mean Squared Difference = ',num2str(msd)]);
disp(['1/dP = ',num2str(1/dP)]);
disp(['|MSD-1/dP| = ',num2str(abs(msd-1/dP))]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part b
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
M=2000;
N=1000;

%Get MNIST data
[trainImg trainLab testImg testLab] = getMNISTDigits(M,N);


%training digits
X=zeros(M,28*28);
for i=1:M
    curDigit=trainImg(:,:,i);
    X(i,:)=curDigit(:);
end

%testing digits
Y=zeros(N,28*28);
for i=1:N
    curDigit=testImg(:,:,i);
    Y(i,:)=curDigit(:);
end


%matrix for distances
dists=zeros(N,M);

%Reduce Dimension
trainRedDim=X*G';
testRedDim=Y*G';

for j=1:M
    train=trainRedDim(j,:);
    for i=1:N
        test = testRedDim(i,:);
        %Compute L2 norm
        dists(i,j)= norm(test-train); 
    end
end


%find matches and imposters
[matches imposters] = matchesAndImposters(dists,testLab,trainLab);

%Compute and plot ROC curves
[~, eer]=drawROCCurve(matches,imposters,.001,'ROC Curve');
 
disp(['Equal Error Rate = ',num2str(eer)]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part c
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Using PCA to reduce the dimensionality

%find principal components of training digits
coeffs=princomp(X);

%compute distances
dists=zeros(N,M);

%Find projections on pc's
trainRed=X*coeffs;
testRed=Y*coeffs;

%Reduce dimension
trainRed=trainRed(:,1:dP);
testRed=testRed(:,1:dP);

for j=1:M
    train=trainRed(j,:);
    for i=1:N
        test=testRed(i,:);
        %Compute L2 norm
        dists(i,j)= norm(test-train);        
    end
end


%find matches and imposters
[matches imposters] = matchesAndImposters(dists,testLab,trainLab);

%Compute and plot ROC curves
[pts eer]=drawROCCurve(matches,imposters,.001,'ROC Curve');
 
disp(['Equal Error Rate = ',num2str(eer)]);



